<?php
#class Newz extends AppModel {
/**
* scrapes newzbin
*/
class Newz {


	public $name = 'Feed';
	public $useTable = null;
	private $ch;
	
	public function beforeFilter() {
	#	$this->ch = $this->setupCurl();
	}
	
	public function findAllByKeyword($keyword)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		curl_setopt($ch, CURLOPT_FAILONERROR, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 20);
		curl_setopt($ch, CURLOPT_URL, $this->buildURL($keyword));
		$html = curl_exec($ch);
		die(var_dump(htmlentities($html)));
		$dom = new DOMDocument;
		
		try {
			$dom->loadHTML($html);
			$xpath = new DOMXPath($dom);
		} catch(Exception $e) {
			echo $e;
		}
	}
	
	private function &setupCurl()
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
		curl_setopt($ch, CURLOPT_FAILONERROR, true);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 20);
		
		return $ch;
	}
	
	private function buildURL($keyword)
	{
		return 'http://v3.newzbin.com/search/query/?area=-1&fpn=p&searchaction=Go&areadone=-1&f&q=' . $keyword;
	}
}